package com.glsc.ngateway.common.base.repo.mysql.flowable;

import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableCopy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author lium
 * @date 2021/10/27
 */
public interface FlowableCopyRepo extends JpaRepository<FlowableCopy, Long>, JpaSpecificationExecutor<FlowableCopy> {

    //    @Query(value = "select * from t_flowable_copy m " +
//            "where m.vc_username_destination = :usernameDestination " +
//            "and m.vc_sysid = :sysid " +
//            "and  (:status IS NULL OR m.c_status = :status ) order by m.c_status asc, m.dt_op_time desc ", nativeQuery = true)
    @Query(value = "select m from FlowableCopy m where (:usernameDestination IS NULL OR m.usernameDestination = :usernameDestination ) " +
            "and  (:title IS NULL OR m.title LIKE CONCAT('%', :title,'%')) " +
            "and  (:sysid IS NULL OR m.sysid = :sysid ) " +
            "and  (:status IS NULL OR m.status = :status ) " +
            "and  (:startUser IS NULL OR m.startUser = :startUser ) " +
            "and  (:startUser IS NULL OR m.startUser = :startUser ) " +
            "and  (:beginDate IS NULL OR m.startTime >= :beginDate ) " +
            "and  (:endDate IS NULL OR m.startTime <= :endDate ) " +
            "and  (coalesce(:processDefinitionKey, null)  IS NULL OR m.processDefinitionKey in (:processDefinitionKey) ) " +
            "order by m.status asc, m.opTime desc "
            , countQuery = "select count(1) from FlowableCopy m where (:usernameDestination IS NULL OR m.usernameDestination = :usernameDestination ) " +
            "and  (:sysid IS NULL OR m.sysid = :sysid ) " +
            "and  (:status IS NULL OR m.status = :status ) " +
            "and  (:startUser IS NULL OR m.startUser = :startUser ) " +
            "and  (:startUser IS NULL OR m.startUser = :startUser ) " +
            "and  (:beginDate IS NULL OR m.startTime >= :beginDate ) " +
            "and  (:endDate IS NULL OR m.startTime <= :endDate ) " +
            "and  (coalesce(:processDefinitionKey, null)  IS NULL OR m.processDefinitionKey in (:processDefinitionKey) ) " +
            "order by m.status asc, m.opTime desc ")
    Page<FlowableCopy> findByUsernameAndSysid(@Param("title") String title
            , @Param("processDefinitionKey") List processDefinitionKey
            , @Param("usernameDestination") String usernameDestination
            , @Param("sysid") String sysid, @Param("status") String status
            , @Param("startUser") String startUser
            , @Param("beginDate") LocalDateTime beginDate
            , @Param("endDate") LocalDateTime endDate
            , Pageable pageable);

    List<FlowableCopy> findByProcessInstanceIdAndUsernameDestinationAndSysid(String processInstanceId, String usernameDestination, String sysid);

    @Query(value = "select * from t_flowable_copy where vc_sysid = :sysId and vc_username_destination = :user and vc_process_instance_id in (:processIdList)", nativeQuery = true)
    List<Map<String, String>> findUserParticipateProcessList(List<String> processIdList, String user, String sysId);

    @Query(value = "select * from t_flowable_copy where vc_process_instance_id in (:processIdList)", nativeQuery = true)
    List<String> getCopyUserList(List<String> processIdList);
}
